<?php

/**
 * @version		$Id: hello.php 15 2009-11-02 18:37:15Z chdemko $
 * @package		Joomla16.Tutorials
 * @subpackage	Components
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @author		Christophe Demko
 * @link		http://joomlacode.org/gf/project/helloworld_1_6/
 * @license		License GNU General Public License version 2 or later
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla modelitem library
jimport('joomla.application.component.modelitem');

/**
 * HelloWorld Model
 */
class bgModelbg extends JModel
{
    var $_total = null;
    var $_pagination = null;
    
    public function __construct(){
    	parent::__construct();
 
        $mainframe = JFactory::getApplication();
 
        // Get pagination request variables
        $limit = $mainframe->getUserStateFromRequest('com_center.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
        $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
 
        // In case limit has been changed, adjust it
        $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
 
        $this->setState('limit', $limit);
        $this->setState('limitstart', $limitstart);
    }
    
    function load($cid){
        $table = & JTable::getInstance('bg','bgTable');
        $table->load($cid);
        return $table;
    }
    
    public function savetemp($data, $addNew = false){
    	$table = & JTable::getInstance('bg','bgTable');
        $table->bind($data);
        if($table->store(false, $addNew)){
            
            //update value default 0
            $up = array();
            if($table->OCBA_TERM_CODE == null){
                $up[] = "OCBA_TERM_CODE = NULL";
            }
            if($table->OCBA_PETT_CODE == null){
                $up[] = "OCBA_PETT_CODE = NULL";
            }
            if($table->OCBA_CLUB_CODE == null){
                $up[] = "OCBA_CLUB_CODE = NULL";
            }
            if($up){
                $up = implode(' , ', $up);
            }else{
                $up = null;
            }
            if($up != null){
                $query = "UPDATE ocba SET  $up WHERE OCBA_CODE = '$table->OCBA_CODE'";
                $this->_db->setQuery($query);
                $this->_db->query(); 
            }
            
            return true;
        }
        
        return false;
    }
    
    public function remove($cid){
        $cid_str = array();
        foreach($cid as $value){
            $cid_str[] = "'".$value."'";
        }
        $cid_str = implode(',', $cid_str);
    	$query = "DELETE FROM ocba WHERE OCBA_CODE IN($cid_str)";
        $this->_db->setQuery($query);
        if($this->_db->query()){
            return true;
        }else{
            return false;
        }
    }
    //Get Image name from id
    public function getImgNameFromID($cid){
        $qr = "SELECT OCBA_IMG FROM ocba WHERE OCBA_CODE = '$cid'";
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObject();
       
       return $result->OCBA_IMG;
    }
    //Check ID exist
    public function checkExistID($cid){
        $qr = "SELECT OCBA_CODE FROM ocba WHERE OCBA_CODE = '$cid'";
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObject();
       
        if(!empty($result)){ 
            return true;
        }
        return false;
    }
    //Check ID exist
    public function isClub($cid){
        $qr = "SELECT OCBA_CODE,OCBA_CLUB_CODE FROM ocba WHERE OCBA_CODE = '$cid'";
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObject();
       
        if(!empty($result)){ 
            if($result->OCBA_CLUB_CODE != null){
                return true;
            }
        }
        return false;
    }
    
    //Get all list OCBA
	function getAll($list = null){

       $qr = "SELECT DISTINCT OCBA_CODE, OCBA_NAME, OCBA_ADD1, OCBA_TEL, OCBA_FAX, OCBA_FLAG, OCBA_WEBF, OCBA_DBSY
                    FROM ocba
                    LEFT JOIN gymt ON OCBA_CODE = GYMT_OCBA_CODE
                    LEFT JOIN pool ON OCBA_CODE = POOL_OCBA_CODE
                    LEFT JOIN dock ON OCBA_CODE = DOCK_OCBA_CODE
                    LEFT JOIN (
                        SELECT PREF_CODE, TOWN_CODE
                        FROM town
                        LEFT JOIN pref ON TOWN_PREF_CODE = PREF_CODE
                        ) AS tb ON tb.TOWN_CODE = OCBA_TOWN_CODE";
                        
        $where = array();
        //check user group
           $aut=new bgAuthorize();
          // check  user login
           $group=$aut->getUserGroup();
           $u=JFactory::getUser();
           $u_ocba= $u->USEM_OCBA_CODE;
           if($group==11)
           {
                $where[] = "OCBA_CODE = '$u_ocba'";
           }
        //search
        if(isset($list['filter_typecc'])){
            if($list['filter_typecc'] == 1){
                $where[] = "OCBA_CODE LIKE '0%'";
            }elseif($list['filter_typecc'] == 2){
                $where[] = "OCBA_CODE LIKE '1%'";
            }
       }
       if(isset($list['filter_pref'])){
            if($list['filter_pref'] != 0){
                $where[] = "PREF_CODE = $list[filter_pref]";
            }
       }
       
       if(!empty($list['filter_namecc'])){
            $where[] = "OCBA_NAME LIKE '%$list[filter_namecc]%'";
       }
       
       if(!empty($list['filter_idcc'])){
            $where[] = "OCBA_CODE = '$list[filter_idcc]'";
       }
       
       if(!empty($list['filter_year'])){
            $where[] = "YEAR(OCBA_SDATE)  = $list[filter_year]";
       }
       
       if($list['filter_clubtype'] != 0){
            $where[] = "OCBA_CLUB_CODE = $list[filter_clubtype]";
       }
       
       //checkbox
       if(isset($list['chb1'])){
            $where[] = "GYMT_OCBA_CODE IS NOT NULL";
       }
       if(isset($list['chb2'])){
            $where[] = "DOCK_OCBA_CODE IS NOT NULL";
       }
       if(isset($list['chb3'])){
            $where[] = "POOL_OCBA_CODE IS NOT NULL";
       }
       if(isset($list['chb4'])){
            $where[] = "POOL_POHK = 1";
       }
       if(isset($list['chb5'])){
            $where[] = "POOL_POHK = 2";
       }
       if(isset($list['chb6'])){
            $where[] = "POOL_POHK = 3";
       }
       
       //user create
       //if(isset($list['user_id'])){
        //    $where[] = "OCBA_USER_CREATE = $list[user_id]";
       //}
       
       if($where){
            $where = ' WHERE '.implode(' AND ', $where);
        }else{
            $where = null;
        }
       // get total number record
       $this->_total = $this->getTotal($where);
         
       $qr .= $where;
       
       // order by
       $qr .= " ORDER BY OCBA_DATE DESC"; 
       
       // pagination
       $qr .= " LIMIT ".$this->getState('limitstart').",".$this->getState('limit');
          
       //echo $qr;
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       
       return $result;
	}
    
    function getTotal($where = null){
        // Load the content if it doesn't already exist
        if ($this->_total === null) {
            
            $query = "SELECT count(DISTINCT OCBA_CODE)
                        FROM ocba
                        LEFT JOIN gymt ON OCBA_CODE = GYMT_OCBA_CODE
                        LEFT JOIN pool ON OCBA_CODE = POOL_OCBA_CODE
                        LEFT JOIN dock ON OCBA_CODE = DOCK_OCBA_CODE
                        LEFT JOIN (
                        SELECT PREF_CODE, TOWN_CODE
                        FROM town
                        LEFT JOIN pref ON TOWN_PREF_CODE = PREF_CODE
                        ) AS tb ON tb.TOWN_CODE = OCBA_TOWN_CODE $where";
            
            $this->_db->setQuery($query);
            $this->_total = $this->_db->loadResult();
        }
        
        return $this->_total;
    } 
    
    function getPagination(){
        // Load the content if it doesn't already exist
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
        }
        return $this->_pagination;
    }
    
    /**
     * Check OCBA_CODE exist
    **/
    public function checkExistOCBA_CODE($OCBA_CODE){
        $qr = "SELECT OCBA_CODE FROM ocba WHERE OCBA_CODE = '$OCBA_CODE'";
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObject();
       
        if(!empty($result)){
            return true;
        }
        return false;
    }
    //Load pref
    function getAllPref(){

	   $qr = "SELECT PREF_CODE,PREF_NAME FROM pref ORDER BY PREF_CODE ASC";
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       
       return $result;
	}
    //Load town
    function getAllTown(){

	   $qr = "SELECT TOWN_CODE,TOWN_NAME FROM town WHERE TOWN_FLAG=1 AND TOWN_WEBF=1";
       // order by
       $qr .= " ORDER BY TOWN_CODE ASC"; 
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       
       return $result;
	}
    //Get all Club type
    function getAllClubType(){

	   $qr = "SELECT CLUB_CODE,CLUB_NAME FROM club WHERE CLUB_FLAG=1";
       // order by
       $qr .= " ORDER BY CLUB_CODE ASC"; 
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       
       return $result;
	}
    //Get all COMA
    function getAllComa(){

	   $qr = "SELECT COMA_CODE,COMA_NAME FROM coma WHERE COMA_FLAG=1";
       // order by
       $qr .= " ORDER BY COMA_CODE ASC"; 
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       
       return $result;
	}
    
    //Get list Term
    function getAllTerm($list = null){

	   $qr = "SELECT TERM_CODE,TERM_NAME,TERM_YEAR FROM term WHERE TERM_FLAG=1";
       // search
       if(!empty($list['TERM_NAME'])){
            $where = " AND TERM_NAME LIKE '%$list[TERM_NAME]%'";
       }else{
            $where = null;
       }
       
       // get total number record
       //$this->_total = $this->getTotalTerm($where);
       
       $qr .= $where;
       
       // order by
       $qr .= " ORDER BY TERM_CODE ASC"; 
       
       // pagination
       //$qr .= " LIMIT ".$this->getState('limitstart').",".$this->getState('limit');
          
       
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       
       return $result;
	}
    //Get Total Term
    function getTotalTerm($where = null){
        // Load the content if it doesn't already exist
        if ($this->_total === null) {
            $query = "SELECT count(*) FROM term $where";
            $this->_db->setQuery($query);
            $this->_total = $this->_db->loadResult();
        }
        
        return $this->_total;
    }
    //paganitation term
    function getPaginationTerm(){
        // Load the content if it doesn't already exist
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination($this->getTotalTerm(), $this->getState('limitstart'), $this->getState('limit') );
        }
        return $this->_pagination;
    }
    
    //Get list Town
    function getAllListTown($list = null){
    
	   $qr = "SELECT TOWN_CODE,TOWN_NAME,TOWN_KANA FROM town WHERE TOWN_FLAG=1 AND TOWN_WEBF=1";
       // search
       if(!empty($list['TOWN_NAME'])){
            $where = " AND (TOWN_NAME LIKE '%$list[TOWN_NAME]%' OR TOWN_CODE = '$list[TOWN_NAME]')";
       }else{
            $where = null;
       }
       
       // get total number record
       //$this->_total = $this->getTotalTown($where);
       
       $qr .= $where;
       
       // order by
       $qr .= " ORDER BY TOWN_CODE ASC"; 
       
       // pagination
       //$qr .= " LIMIT ".$this->getState('limitstart').",".$this->getState('limit');
          
       
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       
       return $result;
	}
    //Get Total Term
    function getTotalTown($where = null){
        // Load the content if it doesn't already exist
        if ($this->_total === null) {
            $query = "SELECT count(*) FROM town $where";
            $this->_db->setQuery($query);
            $this->_total = $this->_db->loadResult();
        }
        
        return $this->_total;
    }
    //paganitation term
    function getPaginationTown(){
        // Load the content if it doesn't already exist
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination($this->getTotalTown(), $this->getState('limitstart'), $this->getState('limit'));
        }
        return $this->_pagination;
    }
    //Get list Pett
    function getAllListPett($list = null){
    
	   $qr = "SELECT PETT_CODE,PETT_NOTE,PEDE_NAME FROM pett INNER JOIN pede on PEDE_CODE = PETT_PEDE_CODE WHERE PETT_FLAG=1";
       // search
       if(!empty($list['PEDE_NAME'])){
            $where = " AND PEDE_NAME LIKE '%$list[PEDE_NAME]%'";
       }else{
            $where = null;
       }
       
       // get total number record
       //$this->_total = $this->getTotalTown($where);
       
       $qr .= $where;
       
       // order by
       $qr .= " ORDER BY PETT_CODE ASC"; 
       
       // pagination
       //$qr .= " LIMIT ".$this->getState('limitstart').",".$this->getState('limit');
          
       
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       
       return $result;
	}
    //Get list Pett
    function getAllListClub($list = null){
    
	   $qr = "SELECT CLUB_CODE,CLUB_NAME FROM club WHERE CLUB_FLAG=1";
       // search
       if(!empty($list['CLUB_NAME'])){
            $where = " AND CLUB_NAME LIKE '%$list[CLUB_NAME]%'";
       }else{
            $where = null;
       }
       
       // get total number record
       //$this->_total = $this->getTotalTown($where);
       
       $qr .= $where;
       
       // order by
       $qr .= " ORDER BY CLUB_CODE ASC"; 
       
       // pagination
       //$qr .= " LIMIT ".$this->getState('limitstart').",".$this->getState('limit');
          
       
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       
       return $result;
	}
    
    /*Get all center */
    function getAllListCenter($list = null){
    
	   $qr = "SELECT OCBA_CODE,OCBA_NAME FROM ocba WHERE OCBA_FLAG=1 AND OCBA_WEBF=1 AND OCBA_CODE LIKE '0%'";
       // search
       if(!empty($list['search_name'])){
            $where = " AND (OCBA_NAME LIKE '%$list[search_name]%' OR OCBA_CODE = '$list[search_name]')";
       }else{
            $where = null;
       }
       
       $qr .= $where;
       
       // order by
       $qr .= " ORDER BY OCBA_CODE ASC"; 
       
       
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       
       return $result;
	}
    
}
